package com.ftg.learn.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.learn.system.entity.Dept;
import com.ftg.learn.system.entity.Emp;
import com.ftg.learn.system.mapper.DeptMapper;
import com.ftg.learn.system.mapper.EmpMapper;
import com.ftg.learn.system.service.IEmpService;
import com.ftg.learn.system.vo.OrganizationVo;
import com.ftg.learn.util.JsonWeb;
import com.ftg.learn.util.JsonWebUtil;
import com.ftg.learn.util.TreeUtil;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author kangjx
 * @since 2023-05-09
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService {


    @Resource
    DeptMapper deptMapper;

    @Override
    public JsonWeb<IPage<Emp>> empList(String empName, Integer empGender, String empEmail, BigInteger empDeptId, Long pageNo, Long pageSize) {
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();

        List<BigInteger> idList = new ArrayList<>();
        if (Objects.nonNull(empDeptId)) {
            TreeUtil<Dept, OrganizationVo> treeUtil = new TreeUtil<>(deptMapper.selectList(null), OrganizationVo.class);
            try {
                OrganizationVo deptTreeVo = treeUtil.getTreeNode(empDeptId);
                if (!Collections.isEmpty(deptTreeVo.getChildren())) {
                    empDeptId = null;
                    treeUtil.listId(deptTreeVo, idList);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        queryWrapper.in(idList.size() > 0, Emp::getDeptId, idList);
        queryWrapper.eq(Objects.nonNull(empDeptId) && idList.size() == 0, Emp::getDeptId, empDeptId);

        //不为空的时候
        queryWrapper.likeRight(Objects.nonNull(empName), Emp::getEmpName, empName);
        queryWrapper.eq(Objects.nonNull(empGender), Emp::getEmpGender, empGender);
        queryWrapper.likeRight(Objects.nonNull(empEmail), Emp::getEmpEmail, empEmail);

        Page<Emp> page = new Page<>(pageNo, pageSize);

        return JsonWebUtil.success(this.page(page, queryWrapper));

//        MobilePhoneDesensitization desensitization = new MobilePhoneDesensitization();
//        List<Emp> empList = empPage.getRecords().stream()
//                .peek(empVo -> empVo.setEmpTel(desensitization.serialize(empVo.getEmpTel())))
//                .collect(Collectors.toList());
//        List<EmpVo> empVos = BeanCopyUtil.copyBeanList(empList, EmpVo.class);
//        PageVo<EmpVo> empVoPageVo = new PageVo<>(empPage.getTotal(), empVos);
//        return ResponseResult.okResult(empVoPageVo);
    }
}
